<template>
  <div>
    <MyHeader>
      <div slot="nav"></div>
    </MyHeader>
    <div class="w">
      <div class="content">
        <div class="account-sidebar">
          <div class="avatar gray-box ">
            <div @click="toInfo()">
              <el-avatar v-if="userInfo.avatar" shape="square" :size="168" :src="'http://127.0.0.1:8000/' + userInfo.avatar"> 
              </el-avatar>

              <el-avatar v-else shape="square" :size="168" src='https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'></el-avatar>
               
              <h5> {{ nickname }} </h5>
            </div>
            <div class="box-inner">
              <ul class="account-nav">
                <li v-for="(item,i) in nav" :key='i' :class="{current:item.name===title}"
                    @click="tab(item)">
                  <a href="javascript:;">{{item.name}}</a></li>
              </ul>
            </div>
          </div>
        </div>
        <div class="account-content">
          <router-view></router-view>
        </div>
      </div>
    </div>
    <MyFooter></MyFooter>
  </div>
</template>

<script>
import MyHeader from '../../common/MHeader.vue'
import MyFooter from '../../common/MFooter.vue'
import { mapState, mapMutations } from 'vuex'

export default {
  name: 'User',
  data () {
    return {
      title: '我的订单',
      nav: [
        {name: '我的订单', path: 'orderList'},
        {name: '账户资料', path: 'information'},
        {name: '收货地址', path: 'addressList'},
        {name: '我的评价', path: 'comment'},
        {name: '售后服务', path: 'support'},
      ],
      // 用户昵称，另外写一个变量是因为双绑，在修改昵称的时候，左边的信息也会变
      nickname: ''
    }
  },
  computed: {
    ...mapState(['userInfo']),
  },
  methods: {
    tab (e) {
      this.$router.push('/user/' + e.path)
    },
    toInfo() {
      this.$router.push('/user/information')
    }
  },
  created () {
    let path = this.$route.path.split('/')[2]
    this.nav.forEach(item => {
      if (item.path === path) {
        this.title = item.name
      }
    })
    this.nickname = this.userInfo.nickname
  },
  watch: {
    $route (to) {
      let path = to.path.split('/')[2]
      this.nav.forEach(item => {
        if (item.path === path) {
          this.title = item.name
        }
      })
    }
  },
  components: {
    MyHeader,
    MyFooter
  }
}
</script>

<style lang="scss" scoped>
@import '../../assets/style/mixin';

.w {
  padding-top: 40px;
}

.content {
  display: flex;
  height: 100%;
}

.account-sidebar {
  width: 210px;
  border-radius: 6px;
  .avatar {
    padding-top: 20px;
    border-radius: 10px;
    text-align: center;
    h5 {
      font-size: 18px;
      line-height: 48px;
      font-weight: 700;
    }
  }
  .account-nav {
    padding-top: 15px;
    li {
      position: relative;
      height: 48px;
      border-top: 1px solid #EBEBEB;
      line-height: 48px;
      &:hover {
        a {
          position: relative;
          z-index: 1;
          height: 50px;
          background-color: #98AFEE;
          line-height: 50px;
          color: #FFF;
        }

      }
      a {
        display: block;
      }
      &.current {
        a {
          position: relative;
          z-index: 1;
          height: 50px;
          background-color: #98AFEE;
          line-height: 50px;
          color: #FFF;
        }
      }

    }
  }
}

.account-content {
  margin-left: 20px;
  flex: 1;
}
</style>